package com.hancher.common.button;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;

import com.hancher.common.R;

public class HrPrimaryButton extends androidx.appcompat.widget.AppCompatButton {
    public HrPrimaryButton(Context context) {
        super(context);
        init();
    }

    public HrPrimaryButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public HrPrimaryButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        initHrClickScale();
        initHrPadding();
        initHrBackground(isClickable());
    }

    void initHrBackground(boolean clickable) {
        if (clickable) {
            setTextColor(getResources().getColor(R.color.btn_primary_font_color));
            setBackgroundResource(R.drawable.btn_primary_radius);
        } else {
            setTextColor(getResources().getColor(R.color.btn_disable_font_color));
            setBackgroundResource(R.drawable.btn_disable_radius);
        }
    }

    void initHrPadding() {
        setPadding(16, 16, 16, 16);
    }

    @SuppressLint({"ClickableViewAccessibility"})
    void initHrClickScale() {
        setOnTouchListener((view, motionEvent) -> {
            if (!isClickable()) {
                return false;
            }
            if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                animate().scaleX(0.9f).scaleY(0.9f).setDuration(150).start();
            } else if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
                animate().scaleX(1f).scaleY(1f).setDuration(150).start();
            }
            return false;
        });
    }

    @Override
    public void setClickable(boolean clickable) {
        super.setClickable(clickable);
        initHrBackground(clickable);
    }
}
